﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BL.Membership;
using BaseLib.Security;
using BL;
using System.Security.Cryptography;

namespace DA
{
    public static class UserExtensions
    {
        private const int USERS_ENCRYPTION_KEY = 2;

        public static bool IsLocked(this User user)
        {
            return user.LoginAttempts >= 5;
        }

        public static IEnumerable<Role> GetRoles(this User user)
        {
            if (string.IsNullOrWhiteSpace(user.RolesString))
                return Enumerable.Empty<Role>();

            var roleIds =
                user.RolesString.Split(',')
                .Select(x => int.Parse(x.Trim()));

            RolesRepository rolesRepository = new RolesRepository();

            return
                rolesRepository.GetEntitySet()
                .Where(x => roleIds.Contains(x.RoleId))
                .ToList();
        }

        public static void SetRoles(this User user, IEnumerable<Role> roles)
        {
            SetRoles(user, roles.Select(x => x.RoleId));
        }

        public static void SetRoles(this User user, IEnumerable<int> roleIds)
        {
            user.RolesString = string.Join(",", roleIds);
        }

        public static UserPermission GetCalculatedPermissions(this User user)
        {
            UserPermission perms = user.GetUserPermissions();

            var roles = user.GetRoles();

            foreach (Role role in roles)
            {
                perms = perms | role.GetPermissions(); // add perms
                //perms = perms | (UserPermission)Enum.ToObject(typeof(UserPermission), long.Parse(role.PermissionsString));
            }

            perms = perms & user.Client.GetPermissions(); // cut perms

            return perms;
        }

        public static UserPermission GetUserPermissions(this User user)
        {
            long perms;
            AESEncryption encryption = new AESEncryption(USERS_ENCRYPTION_KEY);
            string number = encryption.DecryptString(user.PermissionsString);

            UserPermission permissions = UserPermission.None;

            //permissions = (UserPermission)Enum.ToObject(typeof(UserPermission), long.Parse(user.PermissionsString));
            if (long.TryParse(number, out perms))
                permissions = (UserPermission)perms;

            return permissions;
        }

        public static void SetPermissions(this User user, UserPermission perm)
        {
            AESEncryption encryption = new AESEncryption(USERS_ENCRYPTION_KEY);
            user.PermissionsString = encryption.EncryptToString(((long)perm).ToString());
        }
        /*Unused!!*/
        public static void SetPassword(this User user, string pass)
        {
            user.Password = Sha1Encryption.HashCode(pass);
        }
        public static bool CheckPassword(this User user, string pass)
        {
            return user.Password == Sha1Encryption.HashCode(pass);
        }
        /**********/
    }

}
